import unittest
from api.login_api import LoginApi
from common.db_util import DBUtil
from api.emp_api import EmpApi
class TestEmp(unittest.TestCase):
    au_headers = None
    mobile = '11122226666'

    @classmethod
    def setUpClass(cls) -> None:
        print('类前置')
        body = {"mobile": "13800000002",
                "password": "123456"}
        login_obj = LoginApi()
        TestEmp.au_headers = login_obj.get_au_headers(body)
        print(f'获取的请求头为{TestEmp.au_headers}')
        sql = f"delete from bs_user where mobile='{TestEmp.mobile}'"
        n = DBUtil.iud_data(sql)
        print(n)
    @classmethod
    def tearDownClass(cls) -> None:
        print('类后置')
        sql = f"delete from bs_user where mobile='{TestEmp.mobile}'"
        n = DBUtil.iud_data(sql)
        print(n)
    def test_emp(self):
        print('员工管理接口测试')
        print(f'TestEmp.au_headers 使用请求头{TestEmp.au_headers}')
        emp_obj = EmpApi()
        add_body = {"username": "龙哥yyds", "mobile": TestEmp.mobile, "timeOfEntry": "2022-01-01", "formOfEmployment": 1,
                     "workNumber": "7777", "departmentName": "行政部", "departmentId": "1066240303092076544",
                     "correctionTime": "2022-01-30T16:00:00.000Z"}
        resp = emp_obj.add_emp(add_body,TestEmp.au_headers)
        json_data = resp.json()
        print(f'添加员工结果为{json_data}')
        self.assertEqual(200,resp.status_code)
        self.assertEqual(True,json_data.get('success'))
        self.assertEqual(10000,json_data.get('code'))
        self.assertIn('成功',json_data.get('message'))
        emp_id = json_data.get('data').get('id')
        print(f'员工id为{emp_id}')

        resp = emp_obj.query_emp(emp_id,TestEmp.au_headers)
        json_data = resp.json()
        print(f'查询员工结果为{json_data}')
        self.assertEqual(200, resp.status_code)
        self.assertEqual(True, json_data.get('success'))
        self.assertEqual(10000, json_data.get('code'))
        self.assertIn('成功', json_data.get('message'))

        body = {"username":"卷王张蝶"}
        resp =emp_obj.modify_emp(body,emp_id,TestEmp.au_headers)
        json_data = resp.json()
        print(f'修改员工结果为{json_data}')
        self.assertEqual(200, resp.status_code)
        self.assertEqual(True, json_data.get('success'))
        self.assertEqual(10000, json_data.get('code'))
        self.assertIn('成功', json_data.get('message'))

        resp = emp_obj.delete_emp(emp_id,TestEmp.au_headers)
        json_data = resp.json()
        print(f'删除员工结果为{json_data}')
        self.assertEqual(200, resp.status_code)
        self.assertEqual(True, json_data.get('success'))
        self.assertEqual(10000, json_data.get('code'))
        self.assertIn('成功', json_data.get('message'))